<template>
	<view class="container">
		<view class="floating-search-btn" @click="openSearchPopup">
			<u-icon name="search" color="#00B54E" size="25"></u-icon>
			<text>搜索</text>
		</view>

		<view v-for="(item, index) in list" :key="index" class="list-item">
			<view class="item-line">
				<text class="label">生产主体:</text>
				<text class="value">{{ item.entityName }}</text>
			</view>
			<view class="item-line">
				<text class="label">法人名称:</text>
				<text class="value">{{ item.legalPerson }}</text>
			</view>
			<view class="item-line">
				<text class="label">电话号码:</text>
				<text class="value">{{ item.phone }}</text>
			</view>
			<view class="item-line">
				<text class="label">主体地址:</text>
				<text class="value">{{ item.address }}</text>
			</view>
			<view class="item-line">
				<text class="label">产品数量:</text>
				<text class="value">{{ item.productCount }}</text>
			</view>
			<view class="item-line">
				<text class="label">开具数量:</text>
				<text class="value">{{ item.issuanceCount }}</text>
			</view>
			<view class="item-line">
				<text class="label">总数量/重量(公斤):</text>
				<text class="value">{{ item.totalWeight }}</text>
			</view>
		</view>
		
		<u-loadmore :status="status" />

		<u-popup :show="showSearchPopup" @close="showSearchPopup = false" mode="center" width="85%" round="16">
			<view class="popup-content">
				<view class="form-item">
					<text class="form-label">日期:</text>
					<view class="date-range-picker">
						<view class="date-input" @click="openStartDatePicker">
							<text>{{ searchForm.startDate || '开始日期' }}</text>
						</view>
						<text class="separator">~</text>
						<view class="date-input" @click="openEndDatePicker">
							<text>{{ searchForm.endDate || '结束日期' }}</text>
						</view>
					</view>
				</view>
		
				<u-button type="success" shape="circle" @click="handleSearch">搜索</u-button>
			</view>
		</u-popup>
		
		<u-datetime-picker
			:show="showStartDatePicker"
			v-model="startDateValue"
			mode="date"
			@confirm="onStartDateConfirm"
			@cancel="showStartDatePicker = false"
			@close="showStartDatePicker = false"
			:max="searchForm.endDate || maxDate"
		></u-datetime-picker>
		
		<u-datetime-picker
			:show="showEndDatePicker"
			v-model="endDateValue"
			mode="date"
			@confirm="onEndDateConfirm"
			@cancel="showEndDatePicker = false"
			@close="showEndDatePicker = false"
			:min="searchForm.startDate"
			:max="maxDate"
		></u-datetime-picker>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showSearchPopup: false,
				showStartDatePicker: false,
				showEndDatePicker: false,
				startDateValue: Number(new Date()),
				endDateValue: Number(new Date()),
				list: [],
				status: 'loadmore',
				page: 1,
				searchForm: {
					startDate: '',
					endDate: ''
				},
				maxDate: ''
			};
		},
		onLoad() {
			const today = this.getCurrentDate();
			this.maxDate = today;
			
			this.searchForm.startDate = '2023-07-01'; 
			this.searchForm.endDate = '2023-08-09';
			
			this.startDateValue = new Date('2023-07-01').getTime();
			this.endDateValue = new Date('2023-08-09').getTime();

			this.fetchList();
		},
		onReachBottom() {
			if (this.status === 'nomore') return;
			this.page++;
			this.fetchList();
		},
		methods: {
			fetchList() {
				const params = {
					page: this.page,
					pageSize: 5,
					startDate: this.searchForm.startDate,
					endDate: this.searchForm.endDate
				};
				console.log('搜索参数:', params);

				this.status = 'loading';
				const mockData = [{
					id: 1,
					entityName: '六六六生产主体', legalPerson: '六六六', phone: '18408248888',
					address: '四川省成都市', productCount: 2, issuanceCount: 15, totalWeight: 700.0,
				}];
				setTimeout(() => {
					if (this.page === 1) this.list = mockData;
					this.status = this.page >= 1 ? 'nomore' : 'loadmore';
				}, 1000);
			},
			handleSearch() {
				this.showSearchPopup = false;
				this.page = 1;
				this.list = [];
				this.fetchList();
			},
			openSearchPopup() {
				this.showSearchPopup = true;
			},
			
			openStartDatePicker() {
				this.showStartDatePicker = true;
				this.showEndDatePicker = false;
			},
			
			openEndDatePicker() {
				this.showEndDatePicker = true;
				this.showStartDatePicker = false;
			},
			
			onStartDateConfirm(e) {
				this.searchForm.startDate = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
				this.startDateValue = e.value; 
				this.showStartDatePicker = false;
			},
			
			onEndDateConfirm(e) {
				this.searchForm.endDate = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
				this.endDateValue = e.value; 
				this.showEndDatePicker = false;
			},
			getCurrentDate() {
				const date = new Date();
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx;
	}
	.floating-search-btn {
		position: fixed;
		top: 20rpx;
		right: 30rpx;
		z-index: 99;
		display: flex;
		align-items: center;
		padding: 10rpx 20rpx;
		background-color: white;
		border-radius: 50rpx;
		border: 1px solid #00B54E;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
		text {
			font-size: 26rpx;
			color: #00b54e;
			margin-left: 8rpx;
		}
	}
	.list-item {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}
	.item-line {
		display: flex;
		font-size: 28rpx;
		margin-bottom: 24rpx;
		&:last-child {
			margin-bottom: 0;
		}
	}
	.label {
		color: #606266;
		width: 250rpx;
	}
	.value {
		color: #303133;
		font-weight: 500;
	}
	.popup-content{
		padding: 30rpx;
	}
	.form-item {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
		font-size: 28rpx;
		.form-label {
			color: #303133;
			margin-right: 20rpx;
		}
		
		.date-range-picker {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		
		.date-input {
			flex: 1;
			background-color: #f5f5f5;
			border-radius: 8rpx;
			padding: 18rpx 10rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 26rpx;
			color: #303133;
			text-align: center;
		}
		
		.separator {
			margin: 0 15rpx;
			color: #606266;
		}
	}
</style>